package com.ydcycloud.base.controller.common;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.smart.mvc.model.Result;
import com.smart.util.StringUtils;
import com.ydcycloud.sys.util.FileUtil;

@Controller
@RequestMapping("/common/upload")
public class UploadController {

	@SuppressWarnings("rawtypes")
	@RequestMapping("/springUpload")
	@ResponseBody
	public Result springUpload(String pathStr, HttpServletRequest request)
			throws IllegalStateException, IOException {
		Result r = Result.createSuccessResult();
		try {
			int i = 0;
			// 将当前上下文初始化给 CommonsMutipartResolver （多部分解析器）
			CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
					request.getSession().getServletContext());
			// 检查form中是否有enctype="multipart/form-data"
			if (multipartResolver.isMultipart(request)) {
				// 将request变成多部分request
				MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				// 获取multiRequest 中所有的文件名
				Iterator iter = multiRequest.getFileNames();

				while (iter.hasNext()) {
					i++;
					// 一次遍历所有文件
					// MultipartFile file = multiRequest.getFile(iter.next()
					// .toString());
					CommonsMultipartFile file = (CommonsMultipartFile) multiRequest
							.getFile((String) iter.next());
					DiskFileItem fi = (DiskFileItem) file.getFileItem();
					File f = fi.getStoreLocation();
					String extendName = fi.getName().substring(
							fi.getName().lastIndexOf("."));
					if (f != null && StringUtils.isNotBlank(extendName)) {
						r.setData(FileUtil
								.saveVideoFile(pathStr, f, extendName));
						r.setMessage("上传成功");
					}

				}

			}
			if (i == 0) {
				r = Result.createErrorResult().setMessage("请选择上传文件");
			}
		} catch (Exception e) {
			e.printStackTrace();
			r = Result.createErrorResult().setMessage("系统错误");
		}
		return r;
	}

	@RequestMapping("/filesUpload")
	@ResponseBody
	public Result filesUpload(@RequestParam("files") MultipartFile[] files,
			HttpServletRequest request) {
		Result r = Result.createSuccessResult();
		try {
			if (files == null || files.length == 0) {
				r = Result.createErrorResult().setMessage("请选择上传文件");
			} else {
				for (int i = 0; i < files.length; i++) {
					MultipartFile file = files[i];
					// 保存文件
					saveFile(file, request);
				}
				r = Result.createSuccessResult().setMessage("上传成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			r = Result.createErrorResult().setMessage("系统错误");
		}
		return r;
	}

	/**
	 * 
	 * 保存文件
	 * 
	 * @Description: TODO
	 * @param @param file
	 * @param @param request
	 * @param @return
	 * @return boolean
	 * @throws
	 * @author yzw
	 * @date 2017年7月10日
	 */
	private boolean saveFile(MultipartFile file, HttpServletRequest request) {
		// 判断文件是否为空
		if (!file.isEmpty()) {
			try {
				// 文件保存路径
				
				 /* String filePath = request.getSession().getServletContext()
				  .getRealPath("/") + "upload/" + file.getOriginalFilename();*/
				 
				String filePath = "G:/" + file.getOriginalFilename();
				// 转存文件
				file.transferTo(new File(filePath));
				return true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return false;
	}

}
